package multitype.loadmore;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.epfresh.api.R;

import multitype.ItemViewBinder;


public class LoadMoreViewBinder
    extends ItemViewBinder<LoadMoreItem, LoadMoreViewBinder.ViewHolder> {
    View.OnClickListener onFailClick ;
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (onFailClick != null){
                onFailClick.onClick(v);
            }
        }
    };
    private int paddingBottom = 0;
    ViewHolder holder;
    public int getPaddingBottom() {
        return paddingBottom;
    }

    public void setPaddingBottom(int paddingBottom) {
        this.paddingBottom = paddingBottom;
    }

    public View.OnClickListener getOnFailClick() {
        return onFailClick;
    }

    public void setOnFailClick(View.OnClickListener onFailClick) {
        this.onFailClick = onFailClick;
    }

    public void setStatus(LoadMoreItem entity) {
       if (holder != null){
           holder.setData(entity);
       }
    }

    static public int resourceId = R.layout.common_view_load_more;

    public int getResourceId() {
        return resourceId;
    }

    public void setResourceId(int resourceId) {
        this.resourceId = resourceId;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(resourceId, parent, false);
        view.setPadding(0,0,0,paddingBottom);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull LoadMoreItem entity) {
        this.holder = holder;
        holder.setData(entity);
        holder.vLoadFail.setOnClickListener(onClickListener);
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        private View vLoading;
        private View vLoadFail;
        private View vLoadNoMore;
        private TextView tvContent;
        private View rl_bottom;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            if(resourceId==R.layout.home_often_view_load_more)
                rl_bottom = itemView.findViewById(R.id.rl_bottom);

            vLoading = itemView.findViewById(R.id.load_more_loading_view);
            vLoadFail = itemView.findViewById(R.id.load_more_load_fail_view);
            vLoadNoMore = itemView.findViewById(R.id.load_more_load_end_view);
            tvContent = (TextView) itemView.findViewById(R.id.tv_content);
        }


        void setData(@NonNull final LoadMoreItem entity) {
            int loadStatus = entity.getLoadStatus();
            if (loadStatus == LoadMoreItem.LOADING || loadStatus == LoadMoreItem.LOAD_NORMAL){
                vLoading.setVisibility(View.VISIBLE);
                vLoadFail.setVisibility(View.GONE);
                vLoadNoMore.setVisibility(View.GONE);
            }
            if (loadStatus == LoadMoreItem.LOAD_FAIL){
                vLoading.setVisibility(View.GONE);
                vLoadFail.setVisibility(View.VISIBLE);
                vLoadNoMore.setVisibility(View.GONE);
            }
            if (loadStatus == LoadMoreItem.LOAD_NO_MORE){
                vLoading.setVisibility(View.GONE);
                vLoadFail.setVisibility(View.GONE);
                vLoadNoMore.setVisibility(View.VISIBLE);
                if(resourceId==R.layout.home_often_view_load_more)
                    rl_bottom.setVisibility(View.VISIBLE);
                else{
                    tvContent.setText("没有更多");
                }
            }
            if (loadStatus == LoadMoreItem.LOAD_EMPTY){
                vLoading.setVisibility(View.GONE);
                vLoadFail.setVisibility(View.GONE);
                vLoadNoMore.setVisibility(View.VISIBLE);
                if(resourceId==R.layout.home_often_view_load_more)
                    rl_bottom.setVisibility(View.GONE);
                tvContent.setText("暂无记录");
            }
        }
    }
}
